package com.zk.shop.service.Impl;

import com.zk.shop.dao.UserDao;
import com.zk.shop.entity.User;
import com.zk.shop.service.UserService;
import com.zk.shop.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author 来永刚
 * @Date 2019/9/29 0029 上午 10:42
 * @Version 1.0
 **/
public class UserServiceImpl implements UserService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    UserDao userDao = session.getMapper(UserDao.class);
    /**
     * 修改用户
     * @param
     * @return boolean
     */
    @Override
    public boolean updateUser(User user) {
        boolean flag = false;
        try {
            flag= userDao.updateUser(user);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    /**
     * 删除用户
     * @param
     * @return boolean
     */

    @Override
    public boolean deleteUser(User user) {
        boolean flag = false;
        try {
            flag= userDao.updateUser(user);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }

    /**
     * 显示所有用户
     * @return
     */
    @Override
    public List<User> showUser() {
        List<User> userList = new ArrayList<>();
        try {
            UserDao ud = session.getMapper(UserDao.class);
            userList= ud.showUser();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return userList;
    }

    /**
     * 登录
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        User u = null;
        try {
            UserDao userDao = session.getMapper(UserDao.class);
            u = userDao.login(user);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return u;
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public int register(User user) {
        int count = 0;
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        try {
            UserDao userDao = session.getMapper(UserDao.class);
            count= userDao.register(user);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return count;
    }

    /**
     * 检查用户名是否存在
     * @param name
     * @return boolean
     */

    @Override
    public User checkName(String name) {
        return userDao.checkname(name);

    }

    @Override
    public boolean updateUserPwd(User user) {
        boolean flag = false;
        SqlSession session = SqlSessionFactoryUtil.getSqlSession();
        try {
            UserDao userDao = session.getMapper(UserDao.class);
            flag= userDao.updateUserPwd(user);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }
        return flag;
    }


}
